import { ref, computed, onBeforeUnmount } from 'vue'
import dayjs from 'dayjs'

export function useCountDown() {
	const time = ref(0)
	let timer = null

	function start(value) {
		time.value = value

		timer = setInterval(() => {
			time.value--
		}, 1000)
	}

	const formatTime = computed(() => {
		return dayjs.unix(time.value).format('mm分ss秒')
	})

	onBeforeUnmount(() => {
		if (timer) {
			clearInterval(timer)
		}
	})

	return {
		formatTime,
		start,
	}
}
